package com.cote.douhahou.pretloan.apply;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.cote.douhahou.pretloan.beans.LnoaAplAkomapalendyItem;
import com.cote.douhahou.pretloan.beans.LoanApDAkomapalendetailInfo;
import com.cote.douhahou.pretloan.home.HomeActivity;
import com.cote.douhahou.pretloan.network.HttpHelper;
import com.cote.douhahou.pretloan.service.CusServiceAkomapalendDialog;
import com.cote.douhahou.pretloan.R;

import java.util.ArrayList;

public class LoanApplyActivity extends AppCompatActivity {

    private TextView currencyTopTv, amountTopTv, termTopTv;
    private LinearLayout pGroupLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_apply);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);

        SwipeRefreshLayout refreshLayout = findViewById(R.id.ala_refreshview);
        refreshLayout.setOnRefreshListener(() -> {
            refreshLayout.setRefreshing(false);
            HttpHelper.inst().applyDetails(this,this::onAplyDetailResp_akomapalend);
        });

        findViewById(R.id.ala_ib_back).setOnClickListener(v -> onBackPressed());
        findViewById(R.id.ala_ib_customerservice).setOnClickListener(v -> new CusServiceAkomapalendDialog(this).show());

        currencyTopTv = findViewById(R.id.ala_tv_currencytop);
        amountTopTv = findViewById(R.id.ala_tv_amounttop);
        termTopTv = findViewById(R.id.ala_tv_termtop);
        pGroupLayout = findViewById(R.id.ala_layout_param_group);


        findViewById(R.id.ala_btn_submit).setOnClickListener(v -> {
            HttpHelper.inst().submitEvent("APPLY","SUBMIT_CLICK","");
            HttpHelper.inst().startApply(this,this::onLnAplyResp_akomapalend);
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        HttpHelper.inst().submitEvent("APPLY","START","");
        HttpHelper.inst().applyDetails(this,this::onAplyDetailResp_akomapalend);
    }

    @SuppressLint("SetTextI18n")
    public void onAplyDetailResp_akomapalend(){

        String amount = LoanApDAkomapalendetailInfo.instance().getAmount_akomapalend();
        String currency = LoanApDAkomapalendetailInfo.instance().getCurrency_akomapalend();

        amountTopTv.setText(amount);
        currencyTopTv.setText(currency);
        termTopTv.setText(LoanApDAkomapalendetailInfo.instance().getDays_akomapalend());

        refreshParamView_akomapalend();
    }

    @SuppressLint("InflateParams")
    private void refreshParamView_akomapalend(){

        pGroupLayout.removeAllViews();

        ArrayList<LnoaAplAkomapalendyItem> params = LoanApDAkomapalendetailInfo.instance().getDetailItemList_akomapalend();
        View groupView = null;
        LinearLayout groupLayout = null;

        for (LnoaAplAkomapalendyItem lnAplyItem:params){

            if (lnAplyItem.isTitle_akomapalend()){
                if (groupView!=null && groupLayout!=null) pGroupLayout.addView(groupView);
                groupView = LayoutInflater.from(this).inflate(R.layout.view_item_group,null);
                groupLayout = groupView.findViewById(R.id.vibg_layout_group);
                addItem(groupLayout,lnAplyItem);
            }else {
                if (groupView!=null && groupLayout!=null) addItem(groupLayout,lnAplyItem);
            }

        }

        pGroupLayout.addView(groupView);

    }

    private void addItem(LinearLayout group, LnoaAplAkomapalendyItem lnAplyItem){

        @SuppressLint("InflateParams") View item = LayoutInflater.from(this).inflate(R.layout.item_apply_info,null);

        TextView nameTv = item.findViewById(R.id.ilda_tv_name);
        TextView valueTv = item.findViewById(R.id.ilda_tv_value);

        nameTv.setText(lnAplyItem.getShowName_akomapalend());
        valueTv.setText(lnAplyItem.getShowDetail_akomapalend());

        if (lnAplyItem.isTitle_akomapalend()){
            nameTv.setTextColor(Color.parseColor("#FF222222"));
            nameTv.setTypeface(Typeface.DEFAULT_BOLD);
            valueTv.setTextColor(Color.parseColor("#FF703EF3"));
            valueTv.setTypeface(Typeface.DEFAULT_BOLD);
        }

        group.addView(item);
    }

    public void onLnAplyResp_akomapalend(){
        HttpHelper.inst().process(this,this::onProcessResp_akomapalend);
    }

    public void onProcessResp_akomapalend(){
        Intent intent = new Intent(this, HomeActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        finish();
    }

    @Override
    protected void onPause() {
        super.onPause();
        HttpHelper.inst().submitEvent("APPLY","END","");
    }

}